//
//  ProductStandardController.m
//  jang
//
//  Created by lee han on 09/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "ProductStandardController.h"
#import "UIView+UIViewExt.h"
#import "CommonView.h"
#import "CRUserData.h"

@interface ProductStandardController ()

@end

@implementation ProductStandardController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _mTitleView.text = _mTitle;
    mTotalCount = 0;
    
    NSInteger height = 30;
    mStandardViews = [[NSMutableArray alloc] init];
    
    for(int i = 0; i < _mStandardData.count; ++i){
        NSDictionary* info = [_mStandardData objectAtIndex:i];
        StandardInfo* view = [StandardInfo loadFromNib];
        view.mIndex = i;
        view.mStandard.text = [info objectForKey:@"sname"];
        view.mPrice.text = [NSString stringWithFormat:@"%@.00", [info objectForKey:@"price"]];
        
        NSInteger maxcount = 0;
        maxcount = [[info objectForKey:@"displaycount"] integerValue];
        view.mCount.text = [NSString stringWithFormat:@"%ld", maxcount];
        
        view.mStepperCount.text = @"0";
        
        view.mStepper.minimumValue = 0;
        view.mStepper.maximumValue = maxcount;
        view.mStepper.stepValue = 1;
        view.mStepper.value = 0;
        
        CGRect r = view.frame;
        r.origin.y = height;
        height += 76;
        view.frame = r;
        
        [view initView];
        view.mDelegate = self;
        
        [_mContainVIew addSubview:view];
        [mStandardViews addObject:view];
    }
    
    height += 5;
    CGRect rect = _mContainVIew.frame;
    UIView* totalview = [[UIView alloc] initWithFrame:CGRectMake(0, height, rect.size.width, 25)];
    totalview.backgroundColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];
    mTextTotal = [[UILabel alloc] initWithFrame:CGRectMake(220, 2, 100, 21)];
    [mTextTotal setFont:[UIFont systemFontOfSize:12]];
    [mTextTotal setTextColor:[UIColor redColor]];
    [mTextTotal setText:@"合计 ￥0"];
    [totalview addSubview:mTextTotal];
    [_mContainVIew addSubview:totalview];
    
    height += 30;
    height += 80;//两个按钮高度
    
    rect.origin.y = rect.origin.y + rect.size.height - height;
    rect.size.height = height;
    _mContainVIew.frame = rect;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onClickClose:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)onClickAdd:(id)sender {
    if(mTotalCount == 0){
        [CommonView addAlertView:self Text:@"请先添加数量"];
        return;
    }
    
    NSMutableDictionary* senddata = [[NSMutableDictionary alloc] init];
    NSMutableArray* selects = [[NSMutableArray alloc] init];
    for (StandardInfo* info in mStandardViews) {
        if(info.mStepper.value != 0){
            NSMutableDictionary* oneselect = [[NSMutableDictionary alloc] init];
            [oneselect setValue:[[_mStandardData objectAtIndex:info.mIndex] objectForKey:@"id"] forKey:@"id"];
            [oneselect setValue:info.mStepperCount.text forKey:@"count"];
            
            [selects addObject:oneselect];
        }
    }
    
    [senddata setValue:selects forKey:@"add"];
 
    [[network sharedInstance] startHttpRequest:self Data:senddata functionType:NetworkFunctionType_AddShopCar];
}

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary*)response functionType:(NSInteger)type{
    if(type == NetworkFunctionType_AddShopCar){
        if([[response objectForKey:@"status"] integerValue] == 1){
            //goto productinfo view
            for (StandardInfo* info in mStandardViews) {
                NSString* pid = [[_mStandardData objectAtIndex:info.mIndex] objectForKey:@"id"];
                NSString* count = info.mStepperCount.text;
                [[CRUserData sharedInstance] addShopcar:pid count:count];
            }
            
            [CommonView addMessageView:@"添加成功"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else{
            [CommonView addAlertView:self Text:@"添加到购物车失败"];
        }
    }
}
//end networkDelegate

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark StandardDelegate
-(void)onStepValueChange:(NSInteger)value{
    mTotalCount = 0;
    for (StandardInfo* info in mStandardViews) {
        NSInteger count = [info.mStepperCount.text integerValue];
        NSInteger price = [info.mPrice.text integerValue];
        mTotalCount += (count * price);
    }
    [mTextTotal setText:[NSString stringWithFormat:@"合计 ￥%ld", mTotalCount]];
}
//end StandardDelegate

@end
